/*
 * Copyright (c) 2012 swing-on-fire Team
 * 
 * This file is part of Swing-On-Fire (http://code.google.com/p/swing-on-fire), licensed under the terms of the MIT
 * License (MIT).
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.google.code.swingonfire.image;

import java.awt.AlphaComposite;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;

/**
 * A DoubleBuffer is an image for double buffering. It automatically creates a compatible image and resizes it if
 * necessary.
 * 
 * @author thred
 */
public class DoubleBufferImage extends Image
{

    private final int transparency;

    private GraphicsConfiguration graphicsConfiguration;
    private Image image = null;
    private boolean modified;

    public DoubleBufferImage()
    {
        this(null, Transparency.OPAQUE);
    }

    public DoubleBufferImage(int transparency)
    {
        this(null, transparency);
    }

    public DoubleBufferImage(GraphicsConfiguration graphicsConfiguration)
    {
        this(graphicsConfiguration, Transparency.OPAQUE);
    }

    public DoubleBufferImage(GraphicsConfiguration graphicsConfiguration, int transparency)
    {
        super();

        this.graphicsConfiguration = graphicsConfiguration;
        this.transparency = transparency;
    }

    public int getTransparency()
    {
        return transparency;
    }

    protected Image ensureImage(Component component)
    {
        int width = component.getWidth();
        int height = component.getHeight();
        Image image = getImage();

        if ((width <= 0) || (height <= 0) || (!component.isVisible()))
        {
            setImage(null);
        }
        else if ((image == null) || (image.getWidth(null) != width) || (image.getHeight(null) != height))
        {
            if (graphicsConfiguration == null)
            {
                graphicsConfiguration = component.getGraphicsConfiguration();
            }

            setImage(createImage(graphicsConfiguration, width, height, image));
        }

        return getImage();
    }

    protected Image ensureImage(int width, int height)
    {
        Image image = getImage();

        if ((width <= 0) || (height <= 0))
        {
            setImage(null);
        }
        else if ((image == null) || (image.getWidth(null) != width) || (image.getHeight(null) != height))
        {
            setImage(createImage(graphicsConfiguration, width, height, image));
        }

        return getImage();
    }

    protected Image createImage(GraphicsConfiguration graphicsConfiguration, int width, int height, Image previousImage)
    {
        BufferedImage result = createImage(graphicsConfiguration, width, height);
        Graphics2D g = result.createGraphics();

        paint(g, width, height, previousImage);

        return result;
    }

    protected BufferedImage createImage(GraphicsConfiguration graphicsConfiguration, int width, int height)
    {
        if (graphicsConfiguration != null)
        {
            return graphicsConfiguration.createCompatibleImage(width, height, transparency);
        }

        switch (transparency)
        {
            case Transparency.OPAQUE:
                return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            case Transparency.BITMASK:
                return new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED);

            default:
                return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        }
    }

    protected void paint(Graphics2D g, int width, int height, Image previousImage)
    {
        Graphics2D gClear = (Graphics2D) g.create();

        gClear.setComposite(AlphaComposite.Clear);
        gClear.fillRect(0, 0, width, height);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getWidth(ImageObserver observer)
    {
        return (image != null) ? image.getWidth(observer) : 0;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getHeight(ImageObserver observer)
    {
        return (image != null) ? image.getHeight(observer) : 0;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ImageProducer getSource()
    {
        return null;
    }

    public Graphics2D getGraphics(Component component)
    {
        Image image = ensureImage(component);

        return (Graphics2D) ((image != null) ? image.getGraphics() : null);
    }

    public Graphics2D getGraphics(int width, int height)
    {
        Image image = ensureImage(width, height);

        return (Graphics2D) ((image != null) ? image.getGraphics() : null);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Graphics getGraphics()
    {
        return (image != null) ? image.getGraphics() : null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object getProperty(String name, ImageObserver observer)
    {
        return null;
    }

    public Image getImage()
    {
        return image;
    }

    public Image getImage(Component component)
    {
        return ensureImage(component);
    }

    public Image getImage(int width, int height)
    {
        return ensureImage(width, height);
    }

    public void setImage(Image image)
    {
        modified = true;

        this.image = image;
    }

    public boolean isModified()
    {
        return modified;
    }

    public void setModified(boolean modified)
    {
        this.modified = modified;
    }

    public void dispose()
    {
        image = null;
    }
}